package com.fooddelivery.Service.Impl;

import com.fooddelivery.Common.Unit.OrderNumberGenerator;
import com.fooddelivery.Common.queryParam.QueryPageParamSaveTOrder;
import com.fooddelivery.Common.responseVO.OrderDetailVO;
import com.fooddelivery.Entity.OrderDish;
import com.fooddelivery.Entity.TOrder;
import com.fooddelivery.Entity.User;
import com.fooddelivery.Mapper.OrderDishMapper;
import com.fooddelivery.Mapper.TOrderMapper;
import com.fooddelivery.Mapper.UserMapper;
import com.fooddelivery.Service.TOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author swy
 * @since 2025-09-09
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {
    @Autowired
    private TOrderMapper tOrderMapper;

    @Autowired
    private OrderNumberGenerator orderNumberGenerator;

    @Autowired
    private OrderDishMapper orderDishMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public OrderDetailVO getOrderDetail(Long id) {
       return   tOrderMapper.getOrderDetail(id);
    }

    @Override
    public List<OrderDetailVO> currentMthOrd() {
        YearMonth currentYearMonth = YearMonth.now();
        LocalDate firstDay = currentYearMonth.atDay(1);
        LocalDate lastDay = currentYearMonth.atEndOfMonth();
        return tOrderMapper.selectByMonth(firstDay, lastDay);
    }

    @Override
    public Boolean saveOrder(QueryPageParamSaveTOrder  queryPageParamSaveTOrder) {
        TOrder torder = new TOrder();
        //首先根据ID获取用户信息
        User user = userMapper.getById(queryPageParamSaveTOrder.getUserId().intValue());
        //设置用户信息
        torder.setBuyer(user.getNickname());
//        torder.setOpenid("111111");
        torder.setPhone(user.getPhone());
        torder.setOrderState((byte) 0);
        torder.setPayState((byte) 0);

        //其他 信息
        torder.setOrderNumber(orderNumberGenerator.generateOrderNumberWithRetry(3));
        torder.setNotes(queryPageParamSaveTOrder.getNotes());
        torder.setAddress(queryPageParamSaveTOrder.getAddress());
        torder.setTotalAmount(queryPageParamSaveTOrder.getTotalAmount());
        LocalDateTime now = LocalDateTime.now();
        torder.setCreateTime(now);
        torder.setUserId(user.getId().toString());
        torder.setUpdateTime(now);

        tOrderMapper.insert(torder);//将订单信息插入数据库

        // 转换VO为实体对象
        List<OrderDish> orderDishes = queryPageParamSaveTOrder.getItems().stream().map(item -> {
            OrderDish orderDish = new OrderDish();
            BeanUtils.copyProperties(item, orderDish);
            //设置订单ID
            orderDish.setOrderId(torder.getId());
            // 设置菜品ID
            orderDish.setDishId(item.getDishId());
            //设置菜品数量
            orderDish.setQuantity(item.getQuantity());
            //设置菜品单价
            orderDish.setSnapshotPrice(item.getPrice());
            return orderDish;
        }).collect(Collectors.toList());

        // 批量保存
        orderDishMapper.saveBatch(orderDishes);


        return  true;
    }

    @Override
    public List<OrderDetailVO> person_orderList(String userId) {
         return tOrderMapper.person_orderList(userId);
    }

    @Override
    public void cancel_order(Long id) {
        tOrderMapper.cancel_order(id);

    }

    @Override
    public void payOrder(TOrder tOrder) {
        tOrderMapper.payOrder(tOrder);
    }
}
